﻿using System;
using System.Runtime.InteropServices;
using System.Diagnostics;

namespace MangosRestarter
{
    /// <summary>
    /// 原生方法调用
    /// </summary>
    internal static class NativeMethods
    {
        [DllImport("user32.dll")]
        public static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);

        [DllImport("user32.dll")]
        public static extern bool IsWindowVisible(IntPtr hWnd);

        [DllImport("user32.dll", SetLastError = true)]
        public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

        public const int SW_HIDE = 0;
        public const int SW_SHOW = 5;
        public const int SW_SHOWNORMAL = 1;
        public const int SW_RESTORE = 9;

        /// <summary>
        /// 隐藏或显示指定进程的所有窗口
        /// </summary>
        public static void SetProcessWindowsVisibility(string processName, bool show)
        {
            try
            {
                var processes = Process.GetProcessesByName(processName);
                foreach (var process in processes)
                {
                    SetProcessWindowsVisibility(process, show);
                }
            }
            catch (Exception ex)
            {
                Logger.Write($"设置进程 {processName} 窗口可见性失败: {ex.Message}");
            }
        }

        /// <summary>
        /// 隐藏或显示指定进程的所有窗口
        /// </summary>
        public static void SetProcessWindowsVisibility(Process process, bool show)
        {
            if (process == null) return;

            try
            {
                foreach (ProcessThread thread in process.Threads)
                {
                    EnumWindows((hWnd, lParam) =>
                    {
                        uint processId;
                        GetWindowThreadProcessId(hWnd, out processId);

                        if (processId == process.Id)
                        {
                            if (show)
                            {
                                ShowWindow(hWnd, SW_SHOWNORMAL);
                                ShowWindow(hWnd, SW_RESTORE);
                            }
                            else
                            {
                                ShowWindow(hWnd, SW_HIDE);
                            }
                        }
                        return true;
                    }, IntPtr.Zero);
                }
            }
            catch (Exception ex)
            {
                Logger.Write($"设置进程 {process.ProcessName} 窗口可见性失败: {ex.Message}");
            }
        }

        /// <summary>
        /// 恢复所有监控进程的窗口显示（包括cmd窗口）
        /// </summary>
        public static void ShowAllMonitoredProcessWindows()
        {
            SetProcessWindowsVisibility("mangosd", true);
            SetProcessWindowsVisibility("realmd", true);
            SetProcessWindowsVisibility("mysqld", true);
            SetProcessWindowsVisibility("mysqld-nt", true);
            SetProcessWindowsVisibility("cmd", true); // 添加cmd窗口显示
        }

        /// <summary>
        /// 隐藏所有监控进程的窗口（包括cmd窗口）
        /// </summary>
        public static void HideAllMonitoredProcessWindows()
        {
            SetProcessWindowsVisibility("mangosd", false);
            SetProcessWindowsVisibility("realmd", false);
            SetProcessWindowsVisibility("mysqld", false);
            SetProcessWindowsVisibility("mysqld-nt", false);
            SetProcessWindowsVisibility("cmd", false); // 添加cmd窗口隐藏
        }

        // 用于枚举窗口的委托和API
        private delegate bool EnumWindowsProc(IntPtr hWnd, IntPtr lParam);

        [DllImport("user32.dll")]
        [return: MarshalAs(UnmanagedType.Bool)]
        private static extern bool EnumWindows(EnumWindowsProc lpEnumFunc, IntPtr lParam);

        [DllImport("user32.dll", SetLastError = true)]
        private static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint processId);
    }
}